package com.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DialogInfo extends DialogBox {

	public static DialogInfo dlg;
	
	private VerticalPanel plDlg;
	private HTML msg;
	private Button btnOK;
	
	protected void onPreviewNativeEvent(Event.NativePreviewEvent event){
		if(27 == event.getNativeEvent().getKeyCode()) {
			btnOK.click();
		}
	}
	
	public DialogInfo() {
		super();
		
		plDlg = new VerticalPanel();
		msg = new HTML();
		btnOK = new Button(CNStatsFront.get().constants.close(), new ClickHandler() {
			public void onClick(ClickEvent event) {
				dlg.hide();
			}
		});

		plDlg.add(msg);
		plDlg.add(btnOK);
		plDlg.setCellHorizontalAlignment(msg, HasHorizontalAlignment.ALIGN_LEFT);
		plDlg.setCellHorizontalAlignment(btnOK, HasHorizontalAlignment.ALIGN_RIGHT);
		this.add(plDlg);
		this.setAnimationEnabled(true);
	}
		
	public static void info(String caption, String message) {
		if(null == dlg) {
			dlg = new DialogInfo();
		}
		dlg.setText(caption);
		dlg.msg.setHTML(message);
		dlg.setPopupPositionAndShow(new PositionCallback() {

			public void setPosition(int offsetWidth, int offsetHeight) {
				dlg.setPopupPosition(
						(Window.getClientWidth()-dlg.getOffsetWidth())/2,
						(Window.getClientHeight()-dlg.getOffsetHeight())/2);
			}
			
		});
	}

}
